<%@ page language="java"
         contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ page isELIgnored="false" %>
<!-- isELIgnored=true禁用EL表达式 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!-- JSTL格式化标签 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="${pageContext.request.contextPath}/js/jquery.min-2.1.1.js"></script>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/layui-v2.6.13/layui/css/index.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/layui-v2.6.13/layui/css/layui.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/layui-v2.6.13/layui/css/indexstyle.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui-v2.6.13/layui/layui.js"></script>
</head>
<body id="list-cont class= layui-bg-black">
<div class="site-nav-bg">
    <div class="site-nav w1200">
        <p class="sn-back-home">
            <i class="layui-icon layui-icon-home"></i>
            <a href="${pageContext.request.contextPath}/shop/shopPage">首页</a>
        </p>
<%--        <p class="sn-back-home">--%>
<%--            <i class="layui-icon layui-icon-home"></i>--%>
<%--            <a href="${pageContext.request.contextPath}/admin/main">后台页面</a>--%>
<%--        </p>--%>
        <div class="sn-quick-menu">
            <div class="sp-cart"><i class="layui-icon layui-icon-form"></i><a style="margin-left: 5px"
                                                                              href="${pageContext.request.contextPath}/shop/goods/orderPage">我的订单</a>
            </div>
            <div class="sp-cart" style="margin-left: 15px"><i class="layui-icon layui-icon-cart-simple"></i><a
                    style="margin-left: 5px" href="${pageContext.request.contextPath}/car/carPage">购物车</a></div>

            <c:if test="${sessionScope.u.id != null}">
                <div style="margin-left: 10px">
                    <a href="${pageContext.request.contextPath}/user/userPage">
                        <div><i class="layui-icon layui-icon-user"></i>&nbsp;&nbsp;欢迎用户:<span>${sessionScope.u.uName}</span></div>
                    </a>
                </div>
            </c:if>
            <c:if test="${sessionScope.u.id == null}">
                <div>
                    <div class="login" style="margin-left: 15px"><i class="layui-icon layui-icon-user"></i><a
                            style="margin-left: 5px" href="${pageContext.request.contextPath}/mall/login">登录</a></div>
                </div>
            </c:if>
        </div>
    </div>
</div>
<tr>
    <div class="layui-row" style="width: 900px; height:900px;margin: 0 auto; background-color: white;">
        <div class="layui-col-md3" style=" width: 200px;height: 200px;padding-top: 50px;">
            <input type="hidden" value="${goodsInfos.id}" id="gid">
            <img style="width: 100%" src="${goodsInfos.imgUrl}"/>
        </div>
        <div class="layui-col-md9" style="padding-top: 50px;">
            <table width="25%">
                <tr>
                    <td>
                        <p class="layui-font-20 layui-font-black">
                            <span>${goodsInfos.gName}</span>
                        </p>
                    </td>
                </tr>
                <tr>
                    <td>库存数量：<span>${goodsInfos.gCount}</span></td>
                </tr>
                <br/>
                <tr>
                    <td>下单数量:
                        <button onclick="subcount()" class="layui-icon layui-icon-reduce-circle"></button>
                        <input id="count" name="buyCount" type="text" style="width: 30px" value="1">
                        <button onclick="addcount()" class="layui-icon layui-icon-add-circle"></button>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div style="color: orangered;font-size: 20px;">
                           单价:<span style="color: red">￥${goodsInfos.gPrice}</span></div>
                    </td>
                </tr>
            </table>
            <div style="margin-top: 40px;">

                <div class="layui-inline layui-font-20" style="width: 15%;">
                    <button class="layui-btn layui-btn-fluid layui-bg-red" onclick="createOrder()">
                        购买
                    </button>
                </div>
                <div class="layui-inline layui-font-20" style="width: 15%;">
                    <button class="layui-btn layui-btn-fluid layui-bg-red" onclick="shopCar()">
                        加入购物车
                    </button>
                </div>
            </div>
        </div>
    </div>
</tr>

</body>
<script>
    function shopCar(){
        var buycount = $("#count").val();
        <%--var userId  = ${sessionScope.userId};--%>
        var params =
            "uId=" + ${uId}+
            "&buyCount="+buycount+
            "&gId="+${goodsInfos.id};
            console.info(params)
        $.ajax({
            url: "${pageContext.request.contextPath}/car/addOneGoodsToCar",//请求地址
            type:"POST",
            data:params,
            success:function(code){//请求成功之后的回调函数，接收服务端响应的数据
                if(code == "1") {
                    alert("加入购物车成功");
                    window.location.href = "${pageContext.request.contextPath}/car/carPage";
                } else {
                    alert("加入购物车失败");
                }
                // window.location.reload();
            },
            error:function(){//请求失败之后的回调函数

            }
        });
    }
    function createOrder() {
        var gcount = $("#count").val();
        if(gcount > ${goodsInfos.gCount}){
            alert("超前当前库存！");
            return;
        }
        var id = $("#gid").val();
        var params = "gId="+${goodsInfos.id}
            +"&buyCount="+gcount;
        window.location.reload();
        window.parent.location.reload();
        window.location.href = "${pageContext.request.contextPath}/shop/goods/payPage?"+params;
    }

    function subcount() {
        var obj = document.getElementById("count");
        if (parseInt(obj.value) > 1) {
            obj.value = parseInt(obj.value) - 1;
        }
    }

    function addcount() {
        var obj = document.getElementById("count");
        obj.value = parseInt(obj.value) + 1;
    }
</script>
</html>
